/**
 * Copyright 2004 Cordys R&D B.V. 
 * 
 * This file is part of the Cordys FTP Connector. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 package com.eibus.util;

import com.eibus.connector.nom.Connector;
import com.eibus.directory.soap.DirectoryException;
import com.eibus.exception.ExceptionGroup;
import com.eibus.util.SynchronousRequestResponseHandler;
import com.eibus.util.logger.CordysLogger;
import com.eibus.util.logger.Severity;
import com.eibus.xml.nom.Node;
import com.eibus.xml.xpath.XPath;

public class ConnectorUtils
{
	private static Connector connector = null;
	private static SynchronousRequestResponseHandler requestHandler = null;
	private static CordysLogger logger = CordysLogger.getCordysLogger(ConnectorUtils.class); 
	
	private static final String GETXMLOBJECT_METHOD = "GetXMLObject";

    private static final String XMLSTORE_NAMESPACE = "http://schemas.cordys.com/1.0/xmlstore";
	
	public static int getFtpConfiguration(String configName, String userDn, String organizationDn, String version, String key) 
	{
		int getXMLObjectResponse = getXMLObject(userDn, organizationDn, version, key);
		
		int ftpConfigNode = Node.duplicate(XPath.getFirstMatch(".//ftpconfiguration[name=\"" + configName + "\"]", null, getXMLObjectResponse));
		
		Node.delete(Node.getRoot(getXMLObjectResponse));
		getXMLObjectResponse = 0;
		
		return ftpConfigNode;
	}
	
	public static int getXMLObject(String userDn, String organizationDn, String version, String key)
	{
		int soapRequest = 0;
		Connector connector = getConnector();
		SynchronousRequestResponseHandler requestHandler = getHandler(connector);
		try
		{
			soapRequest = connector.createSOAPMethod(userDn, organizationDn, XMLSTORE_NAMESPACE, GETXMLOBJECT_METHOD);
			Node.setDataElement(soapRequest, "key", key);
			int keyNode = XPath.getFirstMatch("key", null, soapRequest);
			Node.setAttribute(keyNode, "version", version);
			int soapResponse = requestHandler.sendAndWaitNonTransactional(Node.getRoot(soapRequest));
			
			return soapResponse;
		} catch (DirectoryException de)
		{
			logger.log(Severity.ERROR, "An excetpion has occurred while getting ftp configuration. " + de);
		} catch (InterruptedException ie)
		{
			logger.log(Severity.ERROR, "An excetpion has occurred while getting ftp configuration. " + ie);
		} catch (ExceptionGroup eg)
		{
			logger.log(Severity.ERROR, "An excetpion has occurred while getting ftp configuration. " + eg);
		} finally
		{
			Node.delete(Node.getRoot(soapRequest));
			soapRequest = 0;
		}
		return 0;
	}
	
	public synchronized static SynchronousRequestResponseHandler getHandler(Connector connector)
	{
		if(requestHandler == null)
		{
			requestHandler= SynchronousRequestResponseHandler.getHandler(connector);
		}
		return requestHandler;
	}
	
	public static Connector getConnector()
	{
		try {
			if (connector == null)
			{
				connector = Connector.getInstance("MyInternal");
			}
			
			if (!connector.isOpen())
			{
				connector.open();
			}	
		} catch (ExceptionGroup e) {
			logger.log(Severity.ERROR, "An excetpion has occurred while getting the connector instance" + e);
		} catch (DirectoryException e) {
			logger.log(Severity.ERROR, "An excetpion has occurred while getting the connector instance" + e);
		}
	
		return connector;
	}
}